package com.AAA.service.Impl;

import com.AAA.dao.ITroleMapper;
import com.AAA.dao.IUserMapper;
import com.AAA.entity.LayData;
import com.AAA.entity.MyPage;
import com.AAA.entity.Trole;
import com.AAA.entity.User;
import com.AAA.service.ITroleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TroleServiceimpl implements ITroleService {
    @Autowired
    private ITroleMapper mapper;
    @Autowired
    private IUserMapper mapper1;

    @Override
    public boolean add(Trole trole) {
        int insert=-1;
        if (trole.getRid()==null){
            trole.getRolename();
             insert = mapper.insert(trole);
            }else {
            //updateByPrimaryKeySelectiv为接收的参数为对应于数据库的实体类对象，
            //利用字段的自动匹配进行更新表的操作，如果传入obj对象中的某个属性值为null，则不进行数据库对应字段的更新。
             insert = mapper.updateByPrimaryKeySelective(trole);
        }
        return insert>=0?true:false;
    }

    @Override
    public LayData query(MyPage myPage,Trole trole) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());

        PageInfo pageInfo = new PageInfo(mapper.selectAllrole(trole));

        return new LayData(pageInfo);
    }

    @Override
    public String delete(Trole trole) {
        List<User> users = mapper1.selectfindAll(trole);
        trole.getRid();
        if (users.size()==0){
            Integer delete = mapper.delete(trole);
            return "删除成功";
        }else {
            return "删除失败,该角色已被用户使用";
        }

    }

    @Override
    public PageInfo findAll(MyPage myPage,Trole trole) {
        PageHelper.startPage(myPage.getPage(), myPage.getLimit());
        return new PageInfo(mapper.selectAll());
    }

    @Override
    public LayData queryById(String rid) {
        Trole trole = mapper.selectByPrimaryKey(rid);
        return new LayData(trole.getRolename());
    }
}